package com.lc.cehui.ui.activity.mine;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.request.SuggesttionApi;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.http.response.SuggestTypeBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeedbackActivity extends AppActivity {

    @BindView(R.id.publish_ed_desc)
    EditText mPublishEdDesc;
    @BindView(R.id.publish_text_num)
    TextView mPublishTextNum;
    @BindView(R.id.kind_recy)
    RecyclerView kindRecy;
    //输入框初始值
    private int num = 0;
    //输入框最大值
    public int mMaxNum = 200;
    List<SuggestTypeBean.DataBean> dataarrtBeanList ;
    private BaseQuickAdapter<SuggestTypeBean.DataBean, BaseViewHolder> kindAdapter;
    private int msg_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        suggest_type();
        mPublishEdDesc.addTextChangedListener(new TextWatcher() {
            //记录输入的字数
            private CharSequence wordNum;
            private int selectionStart;
            private int selectionEnd;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //实时记录输入的字数
                wordNum = s;
            }

            @Override
            public void afterTextChanged(Editable s) {

                int number = num + s.length();
                //TextView显示剩余字数
                mPublishTextNum.setText("" + number + "/200");
                selectionStart = mPublishEdDesc.getSelectionStart();
                selectionEnd = mPublishEdDesc.getSelectionEnd();
                //判断大于最大值
                if (wordNum.length() > mMaxNum) {
                    //删除多余输入的字（不会显示出来）
//                        s.delete(selectionStart - 1, selectionEnd);
                    s.delete(mMaxNum, s.length());
                    int tempSelection = selectionEnd;
                    mPublishEdDesc.setText(s);
                    mPublishEdDesc.setSelection(tempSelection);//设置光标在最后
                    //吐司最多输入50字
                    toast("最多输入200字");
                }


            }
        });
        kindRecy.setLayoutManager(new GridLayoutManager(this,3));
        kindAdapter = new BaseQuickAdapter<SuggestTypeBean.DataBean, BaseViewHolder>(R.layout.item_suggesttype,dataarrtBeanList) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, SuggestTypeBean.DataBean dataBean) {

                if (dataBean.isIsgrop()) {
                    baseViewHolder.setBackgroundResource(R.id.text, R.drawable.juxing_yuanjiao_blue1);

                }else {
                    baseViewHolder.setBackgroundResource(R.id.text, R.drawable.juxing_juxing_yuanjiao_gray1);
                }
                              baseViewHolder.setText(R.id.text,dataBean.getTitle());
            }
        };
        kindAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                for (SuggestTypeBean.DataBean bean :dataarrtBeanList){
                    bean.setIsgrop(false);
                }
                SuggestTypeBean.DataBean bean = dataarrtBeanList.get(position);
                bean.setIsgrop(!bean.isIsgrop());

                msg_id = dataarrtBeanList.get(position).getMsg_id();
                kindAdapter.notifyDataSetChanged();

            }
        });
        kindRecy.setAdapter(kindAdapter);
    }

    private void suggesttion() {
        if (CourseUtils.isNullOrEmpty(mPublishEdDesc.getText().toString())){
            toast("请填写反馈内容");
            return;
        }
        EasyHttp.post(this)
                .api(new SuggesttionApi().setUserid(SPUtil.get(this, IntentKey.USERID,0)).setMsg_id(msg_id).setContent(mPublishEdDesc.getText().toString()))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                           toast(data.getMsg());
                        if (data.getCode().equals("200")) {
                            finish();

                        }

                    }
                });
    }

    private void suggest_type() {
        EasyHttp.post(this)
                .api("api/suggest_type")
                .request(new HttpCallback<SuggestTypeBean>(this) {

                    @Override
                    public void onSucceed(SuggestTypeBean data) {

                        if (data.getCode().equals("200")) {
                            dataarrtBeanList=new ArrayList<>();
                            dataarrtBeanList.addAll(data.getData());
                            if (dataarrtBeanList.size()>0){
                                dataarrtBeanList.get(0).setIsgrop(true);
                            }
                            kindAdapter.setNewData(dataarrtBeanList);
                            kindAdapter.notifyDataSetChanged();

                        }

                    }
                });
    }

    @OnClick({R.id.bt_submit, R.id.bt_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_submit:
                suggesttion();
                break;
            case R.id.bt_cancel:
                finish();
                break;
        }
    }


}
